package BS.service;

import BS.data.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class Servlet  extends HttpServlet {
    User user = new User();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求的编码
        request.setCharacterEncoding("UTF-8");
        // 设置响应的编码
        response.setContentType("text/html;charset=UTF-8");
        // 获取action
        String action = request.getParameter("action");
        // 对action进行判断
        List<User> userList1 = null;
        if (action.equals("userList")) {// 如果参数是userList则显示所有用户列表
            // 获取用户列表集合


            try {
                userList1 = user.selectUserAll();
                // 获取请求的session
                HttpSession session = request.getSession();
                // 设置session
                System.out.println(userList1);
                session.setAttribute("userList", userList1);
                // 跳转user_list.jsp页面
                request.getRequestDispatcher("user_list.jsp").forward(request, response);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }


        } else if (action.equals("add")) {// 如果参数是add则是添加用户
            // 获取用户名
            String ids = request.getParameter("id");
            int id = Integer.parseInt(ids);
            String phone = request.getParameter("phone");
            String username = request.getParameter("username");
            // 获取密码
            String address = request.getParameter("address");
            // 添加到数据库
            try {
                user.inter(new User(id, username, phone, address));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            // 对添加结果进行判断

            response.getWriter().println("<script>alert('添加用户成功！');window.location.href='userServlet?action=userList';</script>");

        }else if (action.equals("edit")) {// 如果参数是edit则是将要修改的参数保存到session中传输到edit_user.jsp页面进行显示
            // 获取要修改的id
            String id = request.getParameter("id");
            // 通过id查询用户
            try {
                user = user.selectById(Integer.parseInt(id));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            // 保存到session中，然后在edit_user.jsp中使用
            request.getSession().setAttribute("user", user);
            // 跳转到edit_user.jsp页面
            request.getRequestDispatcher("edit_user.jsp").forward(request, response);
        }else if (action.equals("del")) {// 如果参数是del则是删除用户
            // 获取要删除的id
            String id = request.getParameter("id");
            // 调用数据库方法进行删除操作
            try {
                user.deleteUserById(Integer.parseInt(id));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            // 对删除结果进度判断
            response.getWriter().println("<script>alert('删除用户成功！');window.location.href='userServlet?action=userList';</script>");
        } else if (action.equals("update")) {// 如果参数是update则是将修改后的数据保存到数据库中
            // 获取id
            String id = request.getParameter("id");
            // 获取修改后的用户名
            String username = request.getParameter("username");
            // 获取修改后的密码
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            // 将修改更新到数据库中
            try {
                user.updateUser(new User(Integer.parseInt(id), username, phone, address));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            // 对更新结果进行判断

            response.getWriter().println("<script>alert('编辑用户成功！');window.location.href='userServlet?action=userList';</script>");

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}